package com.bookrecomm.dao;

import com.bookrecomm.entity.RatingDistribution;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.Optional;

/**
 * 评分分布数据访问接口
 */
@Repository
public interface RatingDistributionRepository extends JpaRepository<RatingDistribution, Long> {
    
    /**
     * 根据分类ID和统计日期查询最新的评分分布
     * @param categoryId 分类ID，如果为null则查询所有分类
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @return 评分分布数据
     */
    @Query("SELECT rd FROM RatingDistribution rd " +
           "WHERE (:categoryId IS NULL OR rd.category.id = :categoryId) " +
           "AND rd.statDate BETWEEN :startDate AND :endDate " +
           "ORDER BY rd.statDate DESC")
    Optional<RatingDistribution> findLatestByCategory(
            @Param("categoryId") Long categoryId,
            @Param("startDate") LocalDateTime startDate,
            @Param("endDate") LocalDateTime endDate);
}
